﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RbacMan.Domain.DTO;
using RbacMan.Domain.Entity;
using RbacMan.Repository;
using RbacMan.Service;
using System.Data;
using System.Security.Cryptography;
using System.Xml.Linq;

namespace RbacMan.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RbacManController : ControllerBase
    {
        /// <summary>
        /// 服务层666sds呵呵
        /// </summary>
        DataService service;
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<RbacManController> logger;
        
        public RbacManController(DataService service, ILogger<RbacManController> logger)
        {
            this.service = service;
            this.logger = logger;
        }

        #region y用户模块
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="name">用户名字</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult LoginUser(string name, string pwd)
        {
            var list = service.LoginUser(name, pwd);
            logger.LogInformation("用户登录成功");
            return Ok(list);

        }


        //
        /// <summary>
        /// 用户信息显示
        /// </summary>
        /// <returns>返回集合</returns>
        [HttpGet]
        public IActionResult GetUsers()
        {
            var list = service.GetUsers();
            logger.LogInformation("用户信息显示成功");
            return Ok(list);
        }

        #endregion





        #region 角色管理

        /// <summary>
        /// 角色信息显示-查询-分页
        /// </summary>
        /// <param name="name">姓名查询</param>
        /// <returns>返回集合</returns>
        [HttpGet]
        public IActionResult GetRoles(string? name, int pageindex , int pagesize)
        {
            var list = service.GetRoles(name, pageindex,  pagesize);
            logger.LogInformation("角色信息显示成功");
            return Ok(new {
               list.totalcount,
               list.pagecount,
               list.list,

            });
        }
        /// <summary>
        /// 不同角色的用户信息反填
        /// </summary>
        /// <param name="roleid">角色ID</param>
        /// <returns>返回集合</returns>
        [HttpGet]
        public IActionResult GetRoleUser(int roleid)
        {
            var list = service.GetRoleUser(roleid);
            logger.LogInformation("不同角色的显示不同用户信息");
            return Ok(list);
        }

        /// <summary>
        /// 角色分配用户
        /// </summary>
        /// <param name="roleUser">角色用户对象</param>
        /// <returns>返回分配集合</returns>
        [HttpPost]
        public IActionResult RoleUserAdd(RoleUserDTO roleUser)
        {
            var list = service.RoleUserAdd(roleUser);
            logger.LogInformation(" 角色分配用户成功");
            return Ok(list);
        }
        /// <summary>
        /// 不同角色的菜单信息反填
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleMeuns(int roleid)
        {
            var list = service.GetRoleMeuns(roleid);
            return Ok(list);
        }

        /// <summary>
        /// 角色分配菜单
        /// </summary>
        /// <param name="roleMeunDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult RoleMeunAdd(RoleMeunDTO roleMeunDTO)
        {
            var list = service.RoleMeunAdd(roleMeunDTO);
            logger.LogInformation(" 角色分配菜单成功");
            return Ok(list);
        }
        /// <summary>
        /// 角色修改-反填
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleByid(int roleId)
        {
            var list = service.GetRoleByid(roleId);
            logger.LogInformation("角色修改-反填成功");
            return Ok(list);

        }
        /// <summary>
        /// 角色修改-修改
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult RoleUpdate(Role role)
        {
            var list = service.RoleUpdate(role);
            logger.LogInformation("角色修改成功");
            return Ok(list);
        }
        #endregion



        #region 菜单管理


        /// <summary>
        /// 菜单显示列表显示
        /// </summary>
        /// <returns>返回集合</returns>
        [HttpGet]
        public IActionResult GetMeuns()
        {
            var list = service.GetMeuns();
            logger.LogInformation("菜单显示列表显示成功");
            return Ok(list);
        }

        /// <summary>
        /// 菜单显示级联显示
        /// </summary>
        /// <param name="pid"></param>
        /// <returns>返回集合</returns>
        [HttpGet]

        public IActionResult GetMeunCaseader(int pid)
        {
            var list = service.GetMeunCaseader(pid);
            return Ok(list);
        }

        /// <summary>
        /// 菜单根据不同用户进行显示
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="pid"></param>
        /// <returns>返回集合</returns>
        [HttpGet]
        public IActionResult GetRoleMeunByid(int roleId, int pid = 0)
        {
            var list = service.GetRoleMeunByid(roleId,  pid);
            return Ok(list);

        }


        /// <summary>
        /// 菜单根据不同用户进行显示ls001
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="pid"></param>
        /// <returns>返回集合</returns>
        [HttpGet]
        public IActionResult GetRoleMeunByid1(int roleId, int pid = 0)
        {
            var list = service.GetRoleMeunByid(roleId, pid);
            return Ok(list);

        }
        /// <summary>
        /// 菜单根据不同用户进行显示ls002
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="pid"></param>
        /// <returns>返回集合</returns>
        [HttpGet]
        public IActionResult GetRoleMeunByid2(int roleId, int pid = 0)
        {
            var list = service.GetRoleMeunByid(roleId, pid);
            return Ok(list);

        }


        /// <summary>
        /// 菜单添加
        /// </summary>
        /// <param name="meun"></param>
        /// <returns>返回添加结果</returns>
        [HttpPost]
        public IActionResult MeunAdd(Meun meun)
        {
            var list = service.MeunAdd(meun);
            return Ok(list);
        }

        /// <summary>
        /// 菜单的逻辑删除
        /// </summary>
        /// <param name="meunId"></param>
        /// <returns>返回删除结果</returns>
        [HttpPost]
        public IActionResult MeunDel(int meunId)
        {
            var list = service.MeunDel(meunId);
            logger.LogInformation("菜单的逻辑删除成功");

            return Ok(list); ;

        }


        /// <summary>
        /// 菜单反填
        /// </summary>
        /// <param name="meunId"></param>
        /// <returns>返回集合</returns>
        [HttpGet]

        public IActionResult GetMeunById(int meunId)
        {
            var list = service.GetMeunById(meunId);
            logger.LogInformation("菜单反填成功");

            return Ok(list); 
        }


        /// <summary>
        /// 菜单修改
        /// </summary>
        /// <param name="meun"></param>
        /// <returns>返回修改结果</returns>
        [HttpPost]

        public IActionResult MeunUpdate(Meun meun)
        {
            var list = service.MeunUpdate(meun);
            logger.LogInformation("菜单修改成功");

            return Ok(list); ;
        }




        #endregion
    }
}
